package com.kotlin.baselib.widgets

import android.app.Dialog
import android.content.Context
import android.graphics.drawable.AnimationDrawable
import android.view.Gravity
import android.widget.ImageView
import androidx.annotation.NonNull
import androidx.annotation.StyleRes
import com.kotlin.baselib.R


class ProgressLoading private constructor(@NonNull context: Context, @StyleRes themeResId:Int) :Dialog(context,themeResId) {

    companion object{
       private  lateinit var mDialog:ProgressLoading
       private  var animDrawable:AnimationDrawable?=null
        fun create(context:Context) :ProgressLoading{
            mDialog = ProgressLoading(context, R.style.LightProgressDialog)
            mDialog.setContentView(R.layout.progress_dialog)
            mDialog.setCancelable(true)
            mDialog.setCanceledOnTouchOutside(false)
            val lp =  mDialog.window?.attributes
            lp?.gravity = Gravity.CENTER
            lp?.dimAmount = 0.2f
            mDialog.window?.attributes = lp
            val ivLoading = mDialog.findViewById<ImageView>(R.id.iv_loading)
            animDrawable = ivLoading.background as AnimationDrawable
            return mDialog
        }
    }

     fun showLoading(){
        super.show()
         animDrawable?.start()
    }

     fun hideLoading(){
        super.dismiss()
        animDrawable?.stop()
    }

}